package com.googlecode.gingerman.robot;

import java.util.List;
import com.googlecode.gingerman.world.geometry.Coordinates;
import com.googlecode.gingerman.world.geometry.Vector;

public class SimpleAI {
	private double x,y,x2,y2,width,height,angle;
	private int idCmd;
	private AIRobot robot;
	private AIChassis chassis;
	private AILocator locator;
	private AIWeapon weapon;
	private List<RobotInfo> Robots;
	private List<BulletInfo> Bullets;
	private List<WallInfo> Walls;
	private Radio radio;
	
	
	public SimpleAI(AIRobot robot, AIChassis chassis, AILocator locator, AIWeapon weapon,Radio radio){
		this.robot=robot;
		this.chassis=chassis;
		this.locator=locator;
		this.weapon=weapon;
		this.idCmd=robot.getIDCmd();
		this.radio=radio;
	}
	
	public void getLocator(AILocator locator) {
		height=locator.getWorldHeight();
		width=locator.getWorldWidth();
		Robots=locator.scanRobots();
		Bullets=locator.scanBullets();
		Walls=locator.scanWalls();
	}
	
	public void setWeapon(AIWeapon weapon) {
		weapon.fire();
	}
	
	public void step(){
		angle=locator.getAngle();
		angle+=0.02;
		locator.setAngle(angle);
		
		//добавление объектов в  Radio
		getLocator(locator);
		
		int countLocalEnemy=0,countLocalFriends=0; //количество вражеских роботов на локаторе
		
		int CountOfRobots=Robots.size(), CountOfBullets=Bullets.size(), CountOfWalls=Walls.size(), t=0;
		
		for (int i=0;i<=CountOfWalls-1;i++)
		{
			WallInfo wall = Walls.get(i);
			radio.addWall(wall);
		}
		
		for (int i=0;i<=CountOfRobots-1;i++)
		{
			RobotInfo temprobot = Robots.get(i);
			if (idCmd!=temprobot.getIdCmd()) {
				EnemyRobotInfo enemyrobot= new EnemyRobotInfo(temprobot.getX(),temprobot.getY(),temprobot.getAngle(),temprobot.getRadius(),temprobot.getIdCmd());
				radio.addEnemyRobot(enemyrobot);
				countLocalEnemy++;
			}
			else countLocalFriends++;
		}
		
		//Если есть вражеские роботы на локаторе
		if (countLocalEnemy>0)
		{
			if (countLocalFriends==0){
				
			}
			if (countLocalFriends==1){
				
			}
			
		}
		
		
		
		/*
		for (int i=0;i<=CountOfBullets-1;i++)
		{
			BulletInfo bullet = Bullets.get(i);
			x=bullet.getX();
			y=bullet.getY();
			angle=bullet.getAngle();
		} */
		
			
		x2=chassis.getX();
		y2=chassis.getY();
		if (t==1){
			chassis.setNextSpeed(0);
			Coordinates coordinates=new Coordinates(x,y);
			Vector vector=new Vector(coordinates);
			coordinates=new Coordinates(x2,y2);
			Vector tmp=new Vector(coordinates);
			vector=vector.substract(tmp);
			angle=vector.getAngle();
			chassis.setNextAngle(angle);
			weapon.fire();
		}else{
			chassis.setNextSpeed(5);
			x=width;
			y=height;
			Coordinates coordinates=new Coordinates(x,y);
			Vector vector=new Vector(coordinates);
			coordinates=new Coordinates(x2,y2);
			Vector tmp=new Vector(coordinates);
			vector=vector.substract(tmp);
			if (x-x2>0) angle=vector.getAngle(); else angle=vector.getAngle()+Math.PI;
			chassis.setNextAngle(angle);
		}
		

		
		
		
		
	}
	
}
